package org.example;

// 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`，
// 然后按 Enter 键。现在，您可以在代码中看到空格字符。
public class Main {
    public static void main(String[] args) {
        Cinema a = new Cinema();
        a.zhang.start();
        a.sun.start();
        a.zhao.start();
    }
}
        class TicketSeller //负责卖票的类
        {
            int fiveNumber = 3, tenNumber = 0, twentyNumber = 0;

            public synchronized void sellTicket(int receiveMoney) {
                if (receiveMoney == 5) {
                    fiveNumber = fiveNumber + 1;
                    System.out.println(Thread.currentThread().getName() + "给我5元钱，这是您的1张入场券");
                } else if (receiveMoney == 10) {
                    while (fiveNumber < 1) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "靠边等");
                            wait();
                            System.out.println(Thread.currentThread().getName() + "结束等待");
                        } catch (InterruptedException e) {
                        }
                    }
                    fiveNumber = fiveNumber - 1;
                    tenNumber = tenNumber + 1;
                    System.out.println(Thread.currentThread().getName() + "给我10元钱，找您5 元，这是您的1张入场券");
                } else if (receiveMoney == 20) {
                    while (fiveNumber < 1 || tenNumber < 1) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "靠边等");
                            wait();
                            System.out.println(Thread.currentThread().getName() + "结束等待");
                        } catch (InterruptedException e) {
                        }
                    }
                    fiveNumber = fiveNumber - 1;
                    tenNumber = tenNumber - 1;
                    twentyNumber = twentyNumber + 1;
                    System.out.println(Thread.currentThread().getName() + "给我20元钱，找您一张5元和一张10元，这是您的 1张入场券");
                }
                notifyAll();
            }
        }
            class Cinema implements Runnable {
                Thread zhang, sun, zhao;
                TicketSeller seller;

                Cinema() {
                    zhang = new Thread(this);
                    sun = new Thread(this);
                    zhao = new Thread(this);
                    zhang.setName("张小有");
                    sun.setName("刊大名");
                    zhao.setName("越中堂");
                    seller = new TicketSeller();
                }

                public void run() {
                    if (Thread.currentThread() == zhang) {
                        seller.sellTicket(20);
                    } else if (Thread.currentThread() == sun) {
                        seller.sellTicket(10);
                    } else if (Thread.currentThread() == zhao) {
                        seller.sellTicket(5);
                    }
                }
            }